function AllReady() {
    var mstform = Ext.getCmp('KC_OUTBZTL_Head');
    var dgrid = Ext.getCmp('KC_OUTBZTL_Body');
    var toolbar = Ext.getCmp('toolbar');
    var PhidWarehouse = mstform.queryById("PhidWarehouse");
    var PhidChkpsn = mstform.queryById("PhidChkpsn");
    var user_llr = mstform.queryById("user_llr");
    var user_lylx = mstform.queryById("user_lylx");
    var user_xsht_name = mstform.queryById("user_xsht_name");
    var user_fbht = mstform.queryById("user_fbht")
    var dstore = dgrid.store;
    // toolbar.get('imp').setText('引用采购入库')
        // 获取目标按钮
        // const targetButton = document.getElementById('GcButton-1523-btnEl');
        const targetButton = [...document.querySelectorAll('.x-btn-button')]
        .find(btn => 
            btn.querySelector('.x-btn-inner')?.textContent === '单据引用'
        );
        if (!targetButton) {
            console.error('未找到指定按钮');
        }
        
        // 绑定点击事件
        targetButton.addEventListener('click', function (event) {
            console.log(mstform.queryById("user_lylx").getValue(), mstform.queryById("user_fbht").getValue());
            
            // 验证逻辑
            if (mstform.queryById("user_lylx").getValue() == '1' && !mstform.queryById("user_fbht").getValue()) {
                // 验证失败时阻断
                event.stopImmediatePropagation(); // 阻止其他监听器
                event.preventDefault();           // 阻止默认行为
                Ext.Msg.alert('提示', '领用类型为分包领用时需选择合同！');
                return; // 终止函数
            }
        
            // 验证通过时允许执行原有逻辑
            console.log('所有字段已填写，执行后续操作...');
            
   
        });
        // Ext.getCmp('GcButton-1520').on('click', function(e) {
        //     if (mstform.queryById("user_lylx").getValue() == '1' && !mstform.queryById("user_fbht").getValue()) {
        //         console.log('111111111111');
        //         Ext.Msg.alert('提示', '领用类型为分包领用时需选择合同！');
        //         e.stopEvent(); // ExtJS专用事件阻断
        //         return false;   // ExtJS中返回false也会阻断
        //     }
        // });
    if (otype == 'add') {
        mstform.getItem('user_lylx').setValue('1')
        BatchBindCombox([mstform.getItem('user_lylx')])
        mstform.getItem('PhidClassno').userSetMustInput(true);
        mstform.getItem('user_fbht').userSetMustInput(true);

        execServer('GetHrInfo', { 'id': $appinfo.userID }, function (res) {
            if (res.count > 0) {
                var obj = JSON.parse(res.data);
                user_llr.setValue(obj[0].extendObjects.user_id)
                BatchBindCombox([user_llr])
            }
        });
        console.log(mstform.getItem('user_llr').getValue());
    }
    // if (otype == 'add' || otype == 'edit'|| otype == 'view') {
    //     console.log(mstform.getItem('user_llr').getValue());
    //     BatchBindCombox([user_llr])
    // }

    dgrid.addListener('edit', function (editor, e) {                             //监听单据体字段编辑状态 *edit 为编辑事件（据体汇总更新表头字段）
        if (e.originalValue == e.value) { return; }   
        let sum = 0                           //判断原值与新值是否相同，如果相同则返回
            for (i = 0; i < dstore.getCount(); i++) {                            //for循坏 *dstore.getCount()表示单据体总行数
                sum += Ext.Number.from(dstore.getAt(i).get('TaxMony'));         //变量sum累加
            }    
            console.log(sum);
                                                            
            mstform.getItem('user_material_amt').setValue(sum);                  //变量sum值赋值给表头 user_material_amt
    });
    dstore.on("datachanged", async function (dstore, eOpts) {
        const records = dstore.getRange();
        let sum = 0;
        for (const record of records) {
          try {
            sum += record.data.TaxMony
            mstform.getItem('user_material_amt').setValue(sum);  
          } catch (error) {
            // console.error(error);
          }
        }
      });
    mstform.getItem('user_fbht').on('beforetriggerclick', function (eOp, ignoreBeforeEvent) {
        var pc = mstform.getItem('PhidTrProj').getValue();
        if (Ext.isEmpty(pc)) {
            Ext.Msg.alert('提示', '请先选择项目！');
            mstform.getItem('pc').focus();
            return false;
        } else {
            mstform.getItem('user_fbht').setOutFilter({
                phid_pc: pc //phid_pc 字段是通用帮助中的字段
            });
        }
    });
    mstform.getItem('user_fbht').addListener("helpselected", function () {
        console.log(mstform.getItem('user_fbht').getValue());
        
        execServer(
            "getLlbz",
            { cntid: `'${mstform.getItem('user_fbht').getValue()}'` },
            function (res) {
                console.log(res);

                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    mstform.getItem('PhidClassno').setValue(obj[0].extendObjects.phid)
                    BatchBindCombox([mstform.getItem('PhidClassno')])
                }
            }
        );
        execServer(
            "serachFbs",
            { phid: `'${mstform.getItem('user_fbht').getValue()}'` },
            function (res) {
                console.log(res);
                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    mstform.getItem('PhidCompno').setValue(obj[0].extendObjects.phid_supply_ent)
                    BatchBindCombox([mstform.getItem('PhidCompno')])
                }
            }
        );
        execServer("getCntMInfoByPhid",{phid:user_fbht.getValue()},(res)=>{
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            mstform.queryById("user_phid_customer_ent").setValue(datas.customer_id)
            mstform.queryById("user_jfdw_tel").setValue(datas.user_jfdw_tel)
            mstform.queryById("user_contact_information").setValue(datas.user_contact_information)
            BatchBindCombox([mstform.queryById("user_phid_customer_ent")])
            
        })

    });
    PhidWarehouse.addListener("helpselected", function () {
        execServer(
            "getStoreInfo",
            { store: PhidWarehouse.getValue() },
            function (res) {
                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    PhidChkpsn.setValue(obj[0].extendObjects.manapsn)
                    BatchBindCombox([PhidChkpsn])
                }
            }
        );
    });
    mstform.getItem('user_lylx').addListener('change', function (editor, e) {//值发生变化后立即触发
        if (mstform.getItem('user_lylx').getValue() == 1) {
            mstform.getItem('PhidClassno').userSetMustInput(true);
            mstform.getItem('user_fbht').userSetMustInput(true);
            mstform.getItem('PhidCompno').setVisible(true);
        } else {
            mstform.getItem('PhidClassno').userSetMustInput(false);
            mstform.getItem('user_fbht').userSetMustInput(false);
            mstform.getItem('PhidCompno').setVisible(false);
            mstform.getItem('PhidCompno').setValue('')
        }

    });
    mstform.getItem('PhidTrProj').addListener('change', function (editor, e) {//值发生变化后立即触发
        // mstform.getItem('user_fbht').setValue('');
        // mstform.getItem('PhidClassno').setValue('');
        execServer(
            "GETxs_cnt",
            { pc: mstform.getItem('PhidTrProj').getValue() },
            function (res) {
                const obj = JSON.parse(res.data)[0].extendObjects;
                console.log(obj, "obj");
                user_xsht_name.setValue(obj.phid);
                BatchBindCombox([user_xsht_name]);
            }
        );
    });
    toolbar.get('deleterow').on('click', function () {
        var sum = 0
        for (i = 0; i < dstore.getCount(); i++) {
            sum += Ext.Number.from(dstore.getAt(i).get('TaxMony'), 0);
        }
        mstform.getItem('user_material_amt').setValue(sum);

    })

}

function beforeSaveEdit() {

    var dgrid = Ext.getCmp('KC_OUTBZTL_Body');
    var dstore = dgrid.store;

    for (i = 0; i < dstore.getCount(); i++) {
        var if_kxysc = dstore.getAt(i).get('user_if_kxys');
        var if_qualify = dstore.getAt(i).get('user_if_qualify');

        if (if_kxysc == '1' && if_qualify != '1' ) {

            Ext.Msg.alert('提示', '第' + [i + 1] + '行【检验是否合格】为空或不合格，不能保存！');
            Ext.getCmp('toolbar').get("save").enable();
            return false;

        }

    }
    return true;
}



